package com.zt.daoImpl;

import java.util.List;

import org.apache.log4j.Logger;
import org.jbpm.JbpmContext;
import org.jbpm.JbpmException;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.springframework.stereotype.Repository;
import org.springmodules.workflow.jbpm31.JbpmCallback;
import org.springmodules.workflow.jbpm31.support.JbpmDaoSupport;

import com.zt.dao.ILeaveProcessDao;
import com.zt.data.LeaveVo;

@Repository
public class LeaveProcessDaoImpl  extends JbpmDaoSupport implements ILeaveProcessDao {
	private static Logger logger = Logger.getRootLogger();
	
	public long startLeave(final LeaveVo leaveVo) {
		
		Object obj = this.getTemplate().execute(new JbpmCallback(){
			public Object doInJbpm(JbpmContext jbpmContext) throws JbpmException {
				GraphSession graphSession = jbpmContext.getGraphSession();
				//创建请假流程实例
				ProcessDefinition processDefinition = graphSession.findLatestProcessDefinition("请假条");
				ProcessInstance processInstance = new ProcessInstance(processDefinition);
				//将请假信息注入流程
				processInstance.getContextInstance().setVariable("day", leaveVo.getLday());
				processInstance.getContextInstance().setVariable("reason", leaveVo.getLreason());
				processInstance.getContextInstance().setVariable("begin", leaveVo.getLstart());
				processInstance.getContextInstance().setVariable("name2", leaveVo.getLname());
				//进入“写请假条”节点
				processInstance.signal();
				if(logger.isInfoEnabled()){
					displayNextStep(processInstance);
				}
				processInstance.signal();
				if(logger.isInfoEnabled()){
					displayNextStep(processInstance);
				}
				//返回流程实例ID
				return processInstance.getId();
			}});
		
		return Long.parseLong(obj.toString());
	}

	/**
	 * 我的待办任务
	 * @param actorId
	 * @param processDefinitionName
	 * @return
	 */
	public List<TaskInstance> myWaitingList(String actorId, String processDefinitionName){
		return this.myLeaveList(actorId, processDefinitionName, false);
	}
	
	/**
	 * 我的任务
	 * @param actorId 用户名
	 * @param processDefinitionName 流程定义名称
	 * @param flag 为true表示已完成的任务，为false表示未完成的任务
	 */
	@SuppressWarnings("unchecked")
	private List<TaskInstance> myLeaveList(final String actorId, final String processDefinitionName, final boolean flag){
		List<TaskInstance> list = 
			(List<TaskInstance>)this.getTemplate().execute(new JbpmCallback(){
			public Object doInJbpm(JbpmContext jbpmContext) throws JbpmException {
				String hql = null;
				if(!flag){
//					hql = "select ti from org.jbpm.taskmgmt.exe.PooledActor as pa inner join pa.taskInstances as ti "+
//					" where pa.actorId in(?) and ti.actorId is null and ti.isSuspended <> 1 and ti.isOpen = 1 and "+
//					" ti.processInstance.processDefinition.name=? and ti.end is null";
					
					hql= "from org.jbpm.taskmgmt.exe.TaskInstance as t inner join t.processInstance as pi where t.actorId =? and t.isSuspended <> 1 and t.isOpen = 1"+
							" and pi.processDefinition.name =? and ti.end is null ";
				}else{
//					hql = "select distinct ti from org.jbpm.taskmgmt.exe.PooledActor as pa inner join "+
//					" pa.taskInstances as ti where ti.actorId = ? and ti.isSuspended <> 1 and ti.isOpen <> 1 and "+
//					" ti.processInstance.processDefinition.name=? and ti.end is not null" ;
					
					hql= "from org.jbpm.taskmgmt.exe.TaskInstance as t where t.actorId =? and t.isSuspended <> 1 and t.isOpen <> 1"+
					" and t.processInstance.processDefinition.name =? and ti.end is not null ";
				}
				List<TaskInstance> list = jbpmContext.getSession().createQuery(hql)
					.setString(0, actorId)
					.setString(1, processDefinitionName)
					.list();
				
				return list;
			}});
		return list;
	}
	
	private void displayNextStep(ProcessInstance processInstance){
		logger.info("下一步:" + processInstance.getRootToken().getNode().getName());
	}
	
	
}
